<?php


namespace app\pay\controller;

use app\Controller;
use BsPaySdk\core\BsPay;
use BsPaySdk\core\BsPayTools;
use think\Db;

class Huifu extends Controller
{
    /**
     * 本地生活/商城进件回调
     */
    public function shopnotify()
    {
        \think\facade\Log::error('本地生活/商城进件回调');
        \think\facade\Log::error(json_encode(input(),320));
        $dir = './log/shop/'; //设置目录
        if (!file_exists($dir)) {
            mkdir($dir, 0755, true);
        }
        $data = file_get_contents('php://input'); //获取到加密的字符串
        $jd_time = strtotime('12:00');
        $time = time();
        if ($time <= $jd_time) { //上午数据
            file_put_contents($dir . date('Ymd', time()) . "_1.txt", var_export($data, true) . PHP_EOL, FILE_APPEND);
        } else { //下午数据
            file_put_contents($dir . date('Ymd', time()) . "_2.txt", var_export($data, true) . PHP_EOL, FILE_APPEND);
        }

        //验签
        $str = $data;
        $data = urldecode($str);
        parse_str($data, $result);
        $result = str_replace(' ', '+', $result);
        $merConfig = BsPay::getConfig();
        $resp_data = $result['data'];
        $res = BsPayTools::verifySign(
            $result['sign'],
            $resp_data, $merConfig->rsa_huifu_public_key);

        if ($res) { //验签成功
            $resp_data = json_decode($resp_data, true);
            if (!empty($resp_data['sub_resp_code']) && $resp_data['sub_resp_code'] == '00000000') {
                $audit_status = $resp_data['audit_status'];//审核结果；Y：审核通过；N：审核拒绝；F：失败；
                $audit_desc = $resp_data['audit_desc'];
                $product_id = $resp_data['product_id'];//汇付分配的产品号，示例值：MCS
                $token_no = $resp_data['token_no'];//取现卡序列号，审核通过后返回，取现时使用；
                $req_seq_id = $resp_data['req_seq_id'];//原请求流水号

                $shop = model('shop')->getInfo(['req_seq_id'=>$req_seq_id],'site_id');

                if ($shop['huifu_audit_status']!=1){
                    return false;
                }

                $data = [
                    'huifu_token_no'=>$token_no,
                    'huifu_product_id'=>$product_id,
                    'huifu_audit_desc'=>$audit_desc
                ];
                if ($audit_status=='Y'){
                    $data['huifu_audit_status'] = 2;
                } elseif ($audit_status=='N'){
                    $data['huifu_audit_status'] = 3;
                } elseif ($audit_status=='F'){
                    $data['huifu_audit_status'] = 4;
                }
                $res = model('shop')->update($data,[['req_seq_id','=',$req_seq_id]]);
            }
        }

        //返回处理完成信息
        return true;
    }

    //酒店进件回调
    public function hotelnotify()
    {
        \think\facade\Log::error('酒店进件回调');
        \think\facade\Log::error(json_encode(input(),320));
        $dir = './log/hotel/'; //设置目录
        if (!file_exists($dir)) {
            mkdir($dir, 0755, true);
        }
        $data = file_get_contents('php://input'); //获取到加密的字符串
        $jd_time = strtotime('12:00');
        $time = time();
        if ($time <= $jd_time) { //上午数据
            file_put_contents($dir . date('Ymd', time()) . "_1.txt", var_export($data, true) . PHP_EOL, FILE_APPEND);
        } else { //下午数据
            file_put_contents($dir . date('Ymd', time()) . "_2.txt", var_export($data, true) . PHP_EOL, FILE_APPEND);
        }

        //验签
        $str = $data;
        $data = urldecode($str);
        parse_str($data, $result);
        $result = str_replace(' ', '+', $result);
        $merConfig = BsPay::getConfig();
        $resp_data = $result['data'];
        $res = BsPayTools::verifySign(
            $result['sign'],
            $resp_data, $merConfig->rsa_huifu_public_key);

        if ($res) { //验签成功
            $resp_data = json_decode($resp_data, true);
            if (!empty($resp_data['sub_resp_code']) && $resp_data['sub_resp_code'] == '00000000') {
                $audit_status = $resp_data['audit_status'];//审核结果；Y：审核通过；N：审核拒绝；F：失败；
                $audit_desc = $resp_data['audit_desc'];
                $product_id = $resp_data['product_id'];//汇付分配的产品号，示例值：MCS
                $token_no = $resp_data['token_no'];//取现卡序列号，审核通过后返回，取现时使用；
                $req_seq_id = $resp_data['req_seq_id'];//原请求流水号

                $shop = model('hotel')->getInfo(['req_seq_id'=>$req_seq_id],'site_id');

                if ($shop['huifu_audit_status']!=1){
                    return false;
                }

                $data = [
                    'huifu_token_no'=>$token_no,
                    'huifu_product_id'=>$product_id,
                    'huifu_audit_desc'=>$audit_desc
                ];
                if ($audit_status=='Y'){
                    $data['huifu_audit_status'] = 2;
                } elseif ($audit_status=='N'){
                    $data['huifu_audit_status'] = 3;
                } elseif ($audit_status=='F'){
                    $data['huifu_audit_status'] = 4;
                }
                $res = model('hotel')->update($data,[['req_seq_id','=',$req_seq_id]]);
            }
        }

        //返回处理完成信息
        return true;
    }
}